package com.zhuang.mycodeproject.testDemo;
/*
 * @Author zhuang
 * @Description //TODO: 单例
 * @Date  2022/4/1
 * @Param
 * @return
 **/
public class TestSingletonDemo {
    private static volatile TestSingletonDemo instance = null;//volatile 处理20行指令重排的情况

    public TestSingletonDemo() {
        System.out.println(Thread.currentThread().getName()+"构造方法");
    }

    public static TestSingletonDemo getInstance(){
        if(instance == null){
            synchronized (TestSingletonDemo.class){
                if(instance == null){
                    instance = new TestSingletonDemo();
                }
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        for(int i=0;i<1000000;i++){
            new Thread(()->{
                TestSingletonDemo.getInstance();
            },String.valueOf(i)).start();
        }
    }
}
